const { Controller } = require("egg");
const path = require("path");
const fs = require("fs");
const { respAdapter } = require("../../utils");

class ManagerController extends Controller {
  async list() {
    const { ctx, app, config } = this;
    try {
      const token = ctx.headers.authorization
        ? ctx.headers.authorization.split(" ")[1]
        : "";
      await app.jwt.verify(token, config.jwt.secret);

      const query = ctx.query;
      console.log("😈query", query);
      const list = await this.app.mysql.select("event", {
        where: query,
        orders: [
          ["created_time", "DESC"] // 指定时间字段和降序排序
        ]
      });

      if (!list) {
        ctx.body = respAdapter(null, "数据异常，请重试", false);
      }

      ctx.body = respAdapter({ list });
    } catch (err) {
      ctx.body = respAdapter(null, err, false);
    }
  }

  async detail() {
    const { ctx, app, config } = this;
    try {
      const token = ctx.headers.authorization
        ? ctx.headers.authorization.split(" ")[1]
        : "";
      await app.jwt.verify(token, config.jwt.secret);

      const query = ctx.query;
      const data = await this.app.mysql.get("event", query);
      console.log("detail", query);
      let attachments = await this.app.mysql.select("attachment", {
        where: {
          event_id: query.id
        }
      });

      if (!data) {
        ctx.body = respAdapter(null, "数据异常，请重试", false);
      }

      ctx.body = respAdapter({
        ...data,
        attachments
      });
    } catch (err) {
      ctx.body = respAdapter(null, err, false);
    }
  }

  async create() {
    const { ctx, app, config } = this;
    const { body } = ctx.request;
    try {
      const token = ctx.headers.authorization
        ? ctx.headers.authorization.split(" ")[1]
        : "";
      await app.jwt.verify(token, config.jwt.secret);

      const resp = await this.app.mysql.insert("event", body);
      console.log("😈resp", resp);
      await this.app.mysql.insert("log", {
        created_time: new Date(),
        oprator: body.oprator,
        content: JSON.stringify({
          content: "创建 信用事项记录"
        }),
        event_id: resp.insertId
      });

      ctx.body = respAdapter({}, null, !!resp);
    } catch (err) {
      ctx.body = respAdapter(null, err, false);
    }
  }

  async update() {
    const { ctx, app, config } = this;
    const { body } = ctx.request;
    try {
      const token = ctx.headers.authorization
        ? ctx.headers.authorization.split(" ")[1]
        : "";
      await app.jwt.verify(token, config.jwt.secret);

      const beforeList = await this.app.mysql.select("event", {
        where: { id: body.id }
      });
      console.log("beforeList", beforeList);
      const resp = await this.app.mysql.update("event", body);

      await this.app.mysql.insert("log", {
        created_time: new Date(),
        oprator: body.oprator,
        content: JSON.stringify({
          content: "修改 信用事项记录",
          before: beforeList?.[0],
          after: body
        }),
        event_id: body.id
      });
      ctx.body = respAdapter({}, null, !!resp);
    } catch (err) {
      ctx.body = respAdapter(null, err, false);
    }
  }

  async delete() {
    const { ctx, app, config } = this;
    const { body } = ctx.request;
    try {
      const token = ctx.headers.authorization
        ? ctx.headers.authorization.split(" ")[1]
        : "";
      await app.jwt.verify(token, config.jwt.secret);

      const resp = await this.app.mysql.delete("event", body);

      await this.app.mysql.insert("log", {
        created_time: new Date(),
        oprator: body.oprator,
        content: JSON.stringify({
          content: "删除 信用事项记录"
        }),
        event_id: body.id
      });
      ctx.body = respAdapter({}, null, !!resp);
    } catch (err) {
      ctx.body = respAdapter(null, err, false);
    }
  }
}

module.exports = ManagerController;
